package com.laob.laob_special.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.laob.laob_special.service.HttpService;
import com.laob.laob_special.util.HttpClientPoolUtils;
import com.laob.laob_special.util.HttpClientUtils;
import org.springframework.stereotype.Service;
import sun.security.x509.CertificatePolicyMap;

import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName laob_special
 * @title     HttpServiceImpl   
 * @package    com.laob.laob_special.service.impl  
 * @date   2019/9/9 13:45  
 * @explain
 */
@Service
public class HttpServiceImpl implements HttpService {

    /**
     * 测试请求端口号是否可用(使用多线程)
     *
     * @return
     */
    public List<Integer> testPort(String ipAddress, String type, Integer minPort, Integer maxPort, String method) {
        System.out.println("url:" + ipAddress +"-------->"+ minPort + "~"+maxPort);
        List<Integer> realPorts = new LinkedList<>();
        HttpClientPoolUtils.realPorts = new LinkedList<>();
        HttpClientPoolUtils.flag = true;
        // 总执行次数
        int count = maxPort - minPort;
        int tempMax = minPort;
        //分批次执行
        if (count > 5000) {
            //tempMax = minPort + 1000;
            // 执行批次
            int batch = count / 5000;
            System.out.println("执行总批次--->"+batch);
            // 开始分批次测试端口
            for (int i = 0; i <= batch; i++) {
                if (! HttpClientPoolUtils.flag){
                    return HttpClientPoolUtils.realPorts;
                }
                minPort = tempMax;
                // 如果最小端口号大于等于最大端口号
                if (minPort >= maxPort){
                    break;
                }
                System.out.println("========================第" + i + "次===========================");
                tempMax = minPort + 5000;
                if (tempMax > maxPort) {
                    tempMax = maxPort;
                }
                realPorts = HttpClientPoolUtils.testPort(ipAddress, type, minPort, tempMax, method);
            }
        } else {
            System.out.println("minPort" + minPort);
            System.out.println("maxPort" + maxPort);
            if (minPort >= maxPort){
                return realPorts;
            }
            realPorts = HttpClientPoolUtils.testPort(ipAddress, type, minPort, maxPort, method);
        }
        return realPorts;
    }

    /**
     * 测试某一端口的连通性
     *
     * @param url
     * @param type
     * @return
     */
    public JSONObject sendMsg(String url, String type, String params) {
        JSONObject jsonObject = null;
        if ("post".equalsIgnoreCase(type)) {
            jsonObject = HttpClientUtils.httpPost(url, params);
        } else if ("get".equalsIgnoreCase(type)) {
            jsonObject = HttpClientUtils.httpGet(url);
        }
        return jsonObject;
    }


    /**
     * 停止端口扫描
     */
    public void stopScan(){
        System.out.println("===================收到停止扫描请求================");
        HttpClientPoolUtils.flag = false;
    }

    /**
     * 发送自定义请求头的get请求
     * @param ip 路径
     * @return
     */
    public JSONObject httpGetByCustomerHeader (String type,String ip,String reqHeaderStr)  throws Exception{
        if ("get".equalsIgnoreCase(type)){
            JSONObject jsonObject = HttpClientUtils.httpGetByCustomerHeader(ip, reqHeaderStr);
            return jsonObject;
        } else if ("post".equalsIgnoreCase(type)){
            JSONObject jsonObject = HttpClientUtils.httpPostByCustomerHeader(ip, reqHeaderStr);
            return jsonObject;
        }
        return null;
    }

}
